package com.cssw.fyzb.tps.api.adapay;

import com.cssw.fyzb.tps.config.adapay.AdapayConfig;
import com.huifu.adapay.model.Member;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: yangyiqun
 * @Date: 2021/1/06 22:29
 */
@Function(description = "查询adapay用户", parameters = {
        @Parameter(name = "userId", type = Type.String, description = "用户标识", required = true)
})
@Component
public class GetMemberAdapay implements Action {

    private static final Logger logger = LoggerFactory.getLogger(GetMemberAdapay.class);

    @Autowired
    private AdapayConfig adapayConfig;

    @Override
    public Response execute(Request request) throws Exception {
        String userId = request.getString("userId");

        try {
            Map<String, Object> memberParams = new HashMap<>(10);
            memberParams.put("app_id", adapayConfig.getAppId());
            memberParams.put("member_id", userId);

            Map<String, Object> member = Member.update(memberParams);

            return Response.success().put("data", member);
        } catch (Exception e) {
            logger.error("", e);
            return Response.fail("支付发起失败");
        }
    }
}
